home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / DEMOS / GLIQ / TB.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  2.1 KB  |  124 lines

  1. /*
  2.  *  Simple trackball-like motion adapted (ripped off) from projtex.c
  3.  *  (written by David Yu and David Blythe).  See the SIGGRAPH '96
  4.  *  Advanced OpenGL course notes.
  5.  */
  6.  
  7.  
  8. /* includes */
  9. #include <math.h>
  10. #include <assert.h>
  11. #include <GL/glut.h>
  12. #include "tb.h"
  13. #include "trackball.h"
  14.  
  15. /* globals */
  16. static GLuint    tb_lasttime;
  17.  
  18. float curquat[4];
  19. float lastquat[4];
  20. int beginx, beginy;
  21.  
  22. static GLuint    tb_width;
  23. static GLuint    tb_height;
  24.  
  25. static GLint     tb_button = -1;
  26. static GLboolean tb_tracking = GL_FALSE;
  27. static GLboolean tb_animate = GL_TRUE;
  28.  
  29. static void
  30. _tbAnimate(void)
  31. {
  32.   add_quats(lastquat, curquat, curquat);
  33.   glutPostRedisplay();
  34. }
  35.  
  36. void
  37. _tbStartMotion(int x, int y, int time)
  38. {
  39.   assert(tb_button != -1);
  40.  
  41.   glutIdleFunc(0);
  42.   tb_tracking = GL_TRUE;
  43.   tb_lasttime = time;
  44.   beginx = x;
  45.   beginy = y;
  46. }
  47.  
  48. void
  49. _tbStopMotion(unsigned time)
  50. {
  51.   assert(tb_button != -1);
  52.  
  53.   tb_tracking = GL_FALSE;
  54.  
  55.   if (time == tb_lasttime && tb_animate) {
  56.     glutIdleFunc(_tbAnimate);
  57.   } else {
  58.     if (tb_animate) {
  59.       glutIdleFunc(0);
  60.     }
  61.   }
  62. }
  63.  
  64. void
  65. tbAnimate(GLboolean animate)
  66. {
  67.   tb_animate = animate;
  68. }
  69.  
  70. void
  71. tbInit(GLuint button)
  72. {
  73.   tb_button = button;
  74.   trackball(curquat, 0.0, 0.0, 0.0, 0.0);
  75. }
  76.  
  77. void
  78. tbMatrix(void)
  79. {
  80.   GLfloat m[4][4];
  81.  
  82.   assert(tb_button != -1);
  83.   build_rotmatrix(m, curquat);
  84.   glMultMatrixf(&m[0][0]);
  85. }
  86.  
  87. void
  88. tbReshape(int width, int height)
  89. {
  90.   assert(tb_button != -1);
  91.  
  92.   tb_width  = width;
  93.   tb_height = height;
  94. }
  95.  
  96. void
  97. tbMouse(int button, int state, int x, int y)
  98. {
  99.   assert(tb_button != -1);
  100.  
  101.   if (state == GLUT_DOWN && button == tb_button)
  102.     _tbStartMotion(x, y, glutGet(GLUT_ELAPSED_TIME));
  103.   else if (state == GLUT_UP && button == tb_button)
  104.     _tbStopMotion(glutGet(GLUT_ELAPSED_TIME));
  105. }
  106.  
  107. void
  108. tbMotion(int x, int y)
  109. {
  110.   if (tb_tracking) {
  111.     trackball(lastquat,
  112.       (2.0 * beginx - tb_width) / tb_width,
  113.       (tb_height - 2.0 * beginy) / tb_height,
  114.       (2.0 * x - tb_width) / tb_width,
  115.       (tb_height - 2.0 * y) / tb_height
  116.       );
  117.     beginx = x;
  118.     beginy = y;
  119.     tb_animate = 1;
  120.     tb_lasttime = glutGet(GLUT_ELAPSED_TIME);
  121.     _tbAnimate();
  122.   }
  123. }
  124.